package dryven.persistence;

import java.lang.annotation.Annotation;

import javax.persistence.EntityManager;

import dryven.annotations.Connection;
import dryven.config.DatabaseConfiguration;
import dryven.config.DatabaseConfigurationSource;
import dryven.model.di.DependencyInjector;
import dryven.model.di.LocalThreadStorage;

public class PersistenceDependencyInjectingBinder implements DependencyInjector {

	private DatabaseConfigurationSource _source;
	
	public PersistenceDependencyInjectingBinder(DatabaseConfigurationSource source) {
		_source = source;
	}

	@Override
	public boolean appliesToType(Class<?> type, Annotation[] annotations) {
		return 
			type.equals(EntityManager.class);
	}

	@Override
	public Object load(LocalThreadStorage storage, Class<?> type, Annotation[] annotations) {
		return new EntityManagerUtil(_source, storage).getEntityManagerForType(type);
	}
}
